package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.StoreTransactionDaoLocal;
import com.jubao.ejb.dao.StoreTransactionDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.Brand;
import com.jubao.ejb.entity.StoreTransaction;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.StoreTransactionDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.StoreTransactionDaoRemoteJndiName)
public class StoreTransactionDaoImpl implements StoreTransactionDaoLocal, StoreTransactionDaoRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2765870138809164312L;

	static Logger logger = Logger.getLogger(StoreTransactionDaoImpl.class);

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public StoreTransaction find(long sysId) {
		return em.find(StoreTransaction.class, sysId);
	}

	public StoreTransaction save(StoreTransaction storeTransaction) {
		return em.merge(storeTransaction);
	}

	public StoreTransaction findLatest(long storeSysId) {
		Query q = em.createNamedQuery("StoreTransactionDao.eql.findLatestTransactionByStoreSysId").setParameter("storeSysId",
				storeSysId).setMaxResults(1);
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<StoreTransaction> txList = q.getResultList();
		StoreTransaction result = null;
		if (txList != null && txList.size() > 0) {
			result = txList.get(0);
		}
		return result;
	}

	public long getStoreTransactionCount(long storeSysId) {
		Query q = em.createNamedQuery("StoreTransactionDao.eql.getStoreTransactionCountByStoreSysId").setParameter(
				"storeSysId", storeSysId);
		return (Long) q.getSingleResult();
	}

	public List<StoreTransaction> getStoreTransactionList(long storeSysId, int pgno, int pgsize) {
		Query q = em.createNamedQuery("StoreTransactionDao.eql.getStoreTransactionListByStoreSysId").setParameter(
				"storeSysId", storeSysId);
		return q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
	}

}
